package cn.kgc.vote.controller;

import cn.kgc.vote.pojo.Options;
import cn.kgc.vote.pojo.Subjects;
import cn.kgc.vote.service.VoteService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("vote")
public class VoteController {

    @Reference
    private VoteService voteService;

    @RequestMapping("/showAll")
    public List<Subjects> showAllSubjects() {
        return voteService.showAllSubjects();
    }

    @RequestMapping("/init/{id}")
    public Map<String, Object> init(@PathVariable Integer id) {
        Map<String, Object> map = new HashMap<>();
        //修改查看人数
        Boolean result = voteService.updateViewTimes(id);
        //根据主题编号查询主题详情
        Subjects subjects = voteService.showSubjectById(id);
        //根据主题编号查询所有选项详情
        List<Options> optionsList = voteService.showOptionsBySid(id);
        map.put("view", result);
        map.put("sub", subjects);
        map.put("opt", optionsList);
        return map;
    }

    @RequestMapping("/showOptions/{sid}")
    public List<Options> showOptions(@PathVariable Integer sid) {
        //根据主题编号查询所有选项详情(投票完成时用于显示)
        return voteService.showOptionsBySid(sid);
    }

    @RequestMapping("/votes")
    public boolean updateVotes(@RequestParam(value = "id") Integer id, @RequestParam(value = "sid") Integer sid) {
        //修改投票人数
        boolean resultSub = voteService.updateTotalVotes(sid);
        //修改选项票数
        boolean resultOpt = voteService.updateVote(id);
        return resultOpt && resultSub;
    }

}
